using System.IO;
using CodePlex.DnD4eCampaignManager.CampaignManagerTests.TestUtilities;
using CodePlex.DnD4eCampaignManager.DnD4e.Interfaces;
using CodePlex.DnD4eCampaignManager.Repositories;
using NUnit.Framework;

namespace CodePlex.DnD4eCampaignManager.CampaignManagerTests.RepositoryTests
{
    [TestFixture]
    public class PlayerCharacterRepositoryTests
    {
        private IPlayerCharacter _playerCharacter;
        private PlayerCharacterRepository _playerCharacterRepository;

        [Test]
        public void PlayerCharacterRepositoryCanGetAllCharacterNames()
        {
            Assert.AreEqual(Directory.GetFiles("Characters", "*.pc").Length, _playerCharacterRepository.GetAllNames().Count);
        }

        [Test]
        public void PlayerCharacterRepositoryCreatesAFileNamedForTheCharacterBeingSaved()
        {
            IPlayerCharacter playerCharacter = DefaultTestObjects.TestPlayerCharacter();
            if ( File.Exists(string.Format("Characters\\{0}.pc", playerCharacter.Name)) )
            {
                File.Delete(string.Format("Characters\\{0}.pc", playerCharacter.Name));
            }
            Assert.IsFalse(File.Exists(string.Format("Characters\\{0}.pc", playerCharacter.Name)));
            PlayerCharacterRepository playerCharacterRepository = new PlayerCharacterRepository();
            playerCharacterRepository.Save(playerCharacter);
            Assert.IsTrue(File.Exists(string.Format("Characters\\{0}.pc", playerCharacter.Name)));
        }

        [Test]
        public void PlayerCharacterRepositoryLoadRestoresTheCharacterExactlyAsSaved()
        {
            _playerCharacterRepository.Save(_playerCharacter);
            Assert.AreEqual(_playerCharacter, _playerCharacterRepository.GetByName(_playerCharacter.Name));
        }

        [Test]
        public void PlayerCharacterRepositoryLoadRestoresTheCharacterExactlyAsSaved2()
        {
            _playerCharacterRepository.Save(_playerCharacter);
            Assert.AreEqual(_playerCharacter.ToString(), _playerCharacterRepository.GetByName(_playerCharacter.Name).ToString());
        }

        [SetUp]
        public void SetUp()
        {
            _playerCharacter = DefaultTestObjects.TestPlayerCharacter();
            _playerCharacterRepository = new PlayerCharacterRepository();
        }
    }
}